package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.Machine;
import com.drcoffee.drcoffeemall.entry.MachineAndAttachment;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.entry.Type;
import com.drcoffee.drcoffeemall.mapper.MachineMapper;
import com.drcoffee.drcoffeemall.service.MachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class MachineServiceImpl implements MachineService {

    final
    MachineMapper machineMapper;

    public MachineServiceImpl(MachineMapper machineMapper) {
        this.machineMapper = machineMapper;
    }

    @Override
    public ResponseEntity<Object> getType() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Type> typeList = machineMapper.getType();
        return new ResponseEntity<>(typeList, HttpStatus.OK);
    }

    @Override
    public Result getMachineAndAttachment(List<Integer> id) {
        List<MachineAndAttachment> list = machineMapper.getMachineAndAttachment(id);
        if (list!= null){
            return new Result(1, "SUCCESS", "", list);
        }
        return null;

    }

    @Override
    public Result getMachineId(String part_code, String cn_name,String en_name) {
        int[] list = machineMapper.getMachineId(part_code,cn_name,en_name);
        if (list != null ){
            return new Result(1, "SUCCESS", "", list);
        }
        return new Result(-1, "ERROR", "", null);
    }

    /*
     * 查询机器
     * */
    @Override
    public Result getMachine(Integer type_id, String search) {
        try {
            List<Machine> machineList;
            if (type_id == 0) {
                // All
                machineList = machineMapper.getMachineAll("%" + search + "%");
            } else {
                // 根据系列查询
                machineList = machineMapper.getMachine(type_id, "%" + search + "%");
            }
            return new Result(1, "SUCCESS", "", machineList);
        } catch (Exception e) {
            log.error("===================== getMachine()异常： =====================", e);
            return new Result(-1, "ERROR", e.getMessage(), null);
        }
    }

    /*
     * 根据id查询机器
     * */
    @Override
    public Map<String, Object> getMachineById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Machine machine = machineMapper.getMachineById(id);
            resultMap.put("result", true);
            resultMap.put("machine", machine);
            resultMap.put("message", "Success");
        } catch (Exception e) {
            resultMap.put("result", false);
            resultMap.put("error", e.getMessage());
            log.error("===================== getMachineById()异常： =====================", e);
        }
        return resultMap;
    }

}
